#override("css")
<link href="#springUrl('/static/plugins/zTree/css/metroStyle/metroStyle.css')" rel="stylesheet">
#end

#override("body")
<body class="white-bg">
	<div class="wrapper wrapper-content">
		<div class="panel panel-default">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
	</div>
</body>
#end

#override("js")
<script src="#springUrl('/static/plugins/zTree/js/jquery.ztree.all.js')"></script>
<script type="text/javascript">
	var data = $!{menus};
	var currentIds = "$!{parentId}";
	var pid = $(parent.document).find("#parentId"); 			// 调用页面的存放选中后的input域的id
	var pname = $(parent.document).find("#parentName"); 		// 调用页面的显示选中后名字的input域的id
	var settings = {
		check: {
			enable: true,
			chkStyle: "radio",
			radioType: "all",
			selectedMulti: false
		},
		view: {
			dblClickExpand: false
		},
		data: {
			key: {
				name: 'name'
			},
			simpleData: {
				enable: true,
				idKey: 'id',
				pIdKey: 'pId',
				rootPId: ""
			}
		},
		callback: {
			onClick: onClick,
			onCheck: onCheck
		}
	}
	function onClick(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		if (treeNode.parentT)
			zTree.checkNode(treeNode, treeNode.checked, null, true);
		return false;
	}

	function onCheck(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = zTree.getCheckedNodes(true);
		var v = "";
		var ids = "";
		for (var i = 0, l = nodes.length; i < l; i++) {
			if (i != 0) {
				v += ",";
				ids += ",";
			}
			v += nodes[i].name;
			ids += nodes[i].id;
		}
		pname.val(v);
		pid.val(ids);
		pid.trigger("click");

		parent.getMaxSortByPId(ids);
	}
	function checkParent() {
		if (currentIds == "") {
			return;
		}
		var pids = currentIds.split(",");
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = zTree.getNodes();
		var abc = zTree.transformToArray(nodes);
		var name = "";
		for (var i = 0, l = abc.length; i < l; i++) {
			for (var j = 0; j < pids.length; j++) {
				if (abc[i].id == parseInt(pids[j])) {
					zTree.checkNode(abc[i], true, true);
					name += abc[i].name + ",";
				}
			}
		}
		if (name.length > 0) {
			name = name.substring(0, name.length - 1);
		}
		pname.val(name);
		pid.val(currentIds);
		pid.trigger("click");
	}
	$(document).ready(function () {
		$.fn.zTree.init($("#treeDemo"), settings, data);
		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = treeObj.getNodes();
		// 设置节点展开(默认展开三级)
		for (var i = 0; i < nodes.length; i++) {
			treeObj.expandNode(nodes[i], true, false, true);
			if (nodes[i].isParent) {
				var sNodes = nodes[i].children;
				for (var j = 0; j < sNodes.length; j++) {
					treeObj.expandNode(sNodes[j], true, false, true);

					if (sNodes[j].isParent) {
						var kNodes = sNodes[i].children;
						for (var k = 0; k < kNodes.length; k++) {
							treeObj.expandNode(kNodes[k], true, false, true);
						}
					}
				}
			}
		}
		checkParent();
	});
</script>
#end

#extends("/common/base.html")